﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interpreter.BuiltIns
{
	internal class FunctionRegistry
	{
		private List<NativeFunction> functions = new List<NativeFunction>();

		public void Register(int id, NativeFunction function)
		{
			while (functions.Count <= id)
			{
				functions.Add(null);
			}

			if (functions[id] != null)
			{
				throw new Exception("Native function ID collision: " + id);
			}

			functions[id] = function;
		}

		public NativeFunction[] GenerateLookup()
		{
			return this.functions.ToArray();
		}

		public static FunctionRegistry CreateNewRegistry()
		{
			FunctionRegistry fr = new FunctionRegistry();
			Module[] modules = new Module[] {
				new GameImage(),
				new GameScreen(),
				new GameWindow(),
				new OsFile(),
				new SystemConsole(),
				new SystemMath(),
				new SystemPrimitiveMethods(),
				new SystemRandom(),
			};

			foreach (Module module in modules)
			{
				module.Register(fr);
			}

			return fr;
		}
	}
}
